﻿using System;
using System.Collections.Generic;
using Model;
using System.Linq;
using IHotelDal;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;


namespace HotelDal
{
    public  class RoomsDal:IRoomsDal
    {
        MyDbcontext db = new MyDbcontext();

        /// <summary>
        /// 添加客房
        /// </summary>
        /// <param name="rooms"></param>
        /// <returns></returns>    
        public int AddRoom(RoomList list)
        {
            db.roomsModel.Add(list);
            return db.SaveChanges();
        }

        /// <summary>
        /// 删除客房
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        public int DelRoom(int aid)
        {
            var rid = db.roomsmodel.Find(aid);
            db.roomsmodel.Remove(rid);
            return db.SaveChanges();
        }

        /// <summary>
        /// 修改客房
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>    
        public int UptRoom(RoomList m)
        {
            db.Set<RoomList>().Remove(m);
            return db.SaveChanges();
        }  

        /// <summary>
        /// 显示查询客房信息
        /// </summary>
        /// <returns></returns>
        public List<RoomsModel> RoomsModels()
        {
            List<RoomsModel> list = (from r in db.RoomsModel
                                     join rt in db.RoomType on r.Rstyle equals rt.RTid                  
                                     select new RoomsModel
                                     {
                                         RTid=rt.RTid,                                    
                                         RTname = rt.RTname,
                                         Rid = r.Rid,                                        
                                         Rname = r.Rname,
                                         Rstyle = r.Rstyle,
                                         Rprice = r.Rprice,
                                         Rstate = r.Rstate,
                                         Rtel = r.Rtel,
                                     }
                                  ).ToList();
            return list;
        }
        /// <summary>
        /// 显示客房类型
        /// </summary>
        /// <returns></returns>
        public List<RoomType> RoomsType()
        {
            List<RoomType> list = (from r in db.RoomType
                                   select new RoomType
                                   {
                                       RTid = r.RTid,
                                       RTname = r.RTname
                                   }

                                   ).ToList();                             
            return list;
        }

        
    }
}
